import 'package:flutter/material.dart';
import 'dart:io';
//import 'dart:async';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Move The rubbish bin!',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Move The rubbish bin!'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var t1 = 'waiting';
  var state = 'stop';
  var controller;
  static Socket rasSocket;
  void initconnect() {
    setState(() {
        Socket.connect("10.181.231.209",8888).then((Socket socket) {
        rasSocket = socket;
        t1='connected success';
      }).catchError((e) {
        t1='connected fail';
      });
    });
  }
  void move(){
    setState(() {
      var intr='$state';
      rasSocket.write(intr);
      t1 = 'send $state';
    });
  }
  void closeconnect(){
    setState(() {
      rasSocket.close();
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Padding(
                padding: const EdgeInsets.all(15.0),
                child: new Column(
                  children: [
                    new Text(
                      '$t1',
                      style: TextStyle(fontSize: 30,color: Colors.blue),
                    ),
                  ],
                ),
            ),
            /*
            new Padding(
              padding: const EdgeInsets.all(15.0),
              child:
                  new TextField(
                  controller: controller,
                  keyboardType: TextInputType.number,
                  decoration: InputDecoration(
                    suffixIcon: IconButton(
                      icon: Icon(Icons.close),
                      onPressed: () {
                        controller.clear();
                      },
                    ),
                  ),
                ),
              ),
            */
            new Padding(
              padding: const EdgeInsets.all(10.0),
              child: new ElevatedButton(
                onPressed: initconnect,
                child: Text('Connect'),
              )
            ),
            new Padding(
              padding: const EdgeInsets.all(5.0),
              child: new Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  new Padding(
                    padding: const EdgeInsets.only(top:10),
                    child: GestureDetector(
                      onTapDown: (tapDown) {
                        state = 'forward';
                        move();
                      },
                      onTapUp: (tapUp) {
                        state = 'stop';
                        move();
                      },
                      child: Container(
                        alignment: Alignment(0, 0),
                        color: Colors.blue,
                        height: 28,
                        width: 40,
                        child: new Icon(Icons.arrow_upward, 
                          color: Colors.white,),
                      ),
                    ),
                  ),
                  new Container(
                    child: new Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [ 
                          new Padding(
                            padding: EdgeInsets.all(10),
                            child: GestureDetector(
                              onTapDown: (tapDown) {
                                state = 'left';
                                move();
                              },
                              onTapUp: (tapUp) {
                                state = 'stop';
                                move();
                              },
                              child: Container(
                                alignment: Alignment(0, 0),
                                color: Colors.blue,
                                height: 28,
                                width: 40,
                                child: new Icon(Icons.arrow_back, 
                                  color: Colors.white,),
                              ),
                            ),
                          ),
                          new Padding(
                            padding: EdgeInsets.all(10),
                            child: GestureDetector(
                              onTapDown: (tapDown) {
                                state = 'right';
                                move();
                              },
                              onTapUp: (tapUp) {
                                state = 'stop';
                                move();
                              },
                              child: Container(
                                alignment: Alignment(0, 0),
                                color: Colors.blue,
                                height: 28,
                                width: 40,
                                child: new Icon(Icons.arrow_forward, 
                                  color: Colors.white,),
                              ),
                            ),
                          ),
                      ]
                    ),
                  ),
                  new Padding(
                    padding: const EdgeInsets.only(bottom:10),
                    child: GestureDetector(
                      onTapDown: (tapDown) {
                        state = 'back';
                        move();
                      },
                      onTapUp: (tapUp) {
                        state = 'stop';
                        move();
                      },
                      child: Container(
                        alignment: Alignment(0, 0),
                        color: Colors.blue,
                        height: 28,
                        width: 40,
                        child: new Icon(Icons.arrow_downward,
                          color: Colors.white,),
                      ),
                    ),
                  ),

                  new ElevatedButton(
                    onPressed:(){
                      state = 'stop';
                      move();
                    },
                    child: new Icon(Icons.done,
                    color: Colors.white,),
                  ),
                ]
              )
            ),
            new Padding(
              padding: const EdgeInsets.all(3.0),
              child: new Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  new Padding(
                    padding: const EdgeInsets.only(top:5),
                    child: new ElevatedButton(
                              onPressed:(){
                                    state = 'auto';
                                    move();
                                  },
                              child: Text('Automode'),
                              ),
                  ),
                  new Padding(
                    padding: const EdgeInsets.only(top:5),
                    child: new ElevatedButton(
                              onPressed:(){
                                    state = 'auto_audio';
                                    move();
                                  },
                              child: Text('Automode with Audio'),
                              ),
                  ),
                  new Padding(
                    padding: const EdgeInsets.only(top:5),
                    child: new ElevatedButton(
                              onPressed:(){
                                    state = 'auto_Fist';
                                    move();
                                  },
                              child: Text('Automode with Fist'),
                              ),
                  ),
                  new Padding(
                    padding: const EdgeInsets.only(top:5),
                    child: new ElevatedButton(
                              onPressed:(){
                                    state = 'auto_both';
                                    move();
                                  },
                              child: Text('Automode with All'),
                              ),
                  ),
                  new Padding(
                    padding: const EdgeInsets.only(top:5),
                    child: new ElevatedButton(
                              onPressed:(){
                                    state = 'garbage';
                                    move();
                                  },
                              child: Text('detect garbage'),
                              ),
                  )
                ]
              ),
            ),
            new Padding(
              padding: const EdgeInsets.all(3.0),
              child: new ElevatedButton(
                onPressed: closeconnect,
                child: Text('Close'),
                )
            ),

          ],
        ),
      ),
    );
  }
}
